package com.study.webapp.lock;

public class ThreadLocalTest {

	private static ThreadLocal<Integer> seqCount = new ThreadLocal<Integer>() {
		public Integer initialValue() {
			return 0;
		}
	};

	public int nextSeq() {
		seqCount.set(seqCount.get() + 1);
		return seqCount.get();
	}

	public static void main(String[] args) {
		ThreadLocalTest seqCount = new ThreadLocalTest();

		SeqThread t1 = new SeqThread(seqCount);
		SeqThread t2 = new SeqThread(seqCount);
		SeqThread t3 = new SeqThread(seqCount);
		SeqThread t4 = new SeqThread(seqCount);

		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}

	private static class SeqThread extends Thread {
		private ThreadLocalTest seqCount;

		SeqThread(ThreadLocalTest seqcount) {
			this.seqCount = seqcount;
		}

		public void run() {
			for (int i = 0; i < 3; i++) {
				System.out.println(Thread.currentThread().getName() + " ,seqcount :" + seqCount.nextSeq());
			}
		}

	}

}
